<?php
session_start();
if (!isset($_SESSION['user'])) {
    header("Location: login.php");
    exit;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
        <title>NuFashion Admin Control Panel</title>
        <link rel="stylesheet" href="../css/admin-style.css" type="text/css" media="all" />
        <script type="text/javascript">
            function toggle(source) {
                checkboxes = document.getElementsByName('pro_id[]');
                for each (var checkbox in checkboxes)
                checkbox.checked = source.checked;
            }
		
            function hideMessage() {
                var ok = document.getElementById("msgOK");
                addClass(ok, 'msg-hidden');
            }
		
            function addClass(el, name)
            {
                el.className += (el.className ? ' ' : '') +name;
            }
            
            function submitDelete() {
                var choice = confirm("Bạn có chắc là bạn muốn xóa sản phẩm?");
                if (choice == true) {
                    var form = document.getElementById("form");
                    form.method = "post";
                    form.action = "../action/deleteProductAction.php";
                    form.submit();
                } else {
                    
                }
                
            }
			
        </script>
    </head>
    <body>

        <!-- Header -->
        <?php include('header.php') ?>
        <!-- End Header -->

        <?php
        include('../service/productService.php');
        include("../service/categoryService.php");
        $page_num = $_GET['page_num'];
        if (!isset($page_num)) {
            $page_num = 1;
        }
        $row_per_page = 30;
        $cateID = $_GET['cateID'];

        if (!isset($cateID)) {
            $cateID = "";
        }

        $keyword = $_GET['keyword'];

        if (!isset($keyword)) {
            $keyword = "";
        }

        $total = getNumberOfResult($keyword, $cateID);
        $total_page = ceil($total / $row_per_page);
        $start_position = ($page_num * $row_per_page) - ($row_per_page);
        ?>

        <!-- Container -->
        <div id="container">
            <div class="shell">

                <!-- 
                <div class="small-nav">
                        <a href="#">Dashboard</a>
                        <span>&gt;</span>
                        Current Articles
                </div>
        Small Nav -->
                <!-- End Small Nav -->

                <!-- Message OK -->
                <?php
                $message = "";
                $deleteMessage = $_GET['delete_message'];
                if (isset($deleteMessage)) {
                    if ($deleteMessage == 'ok') {
                        $message = 'Sản phẩm đã được xóa thành công!';
                    } else {
                        $message = 'Không thể xóa sản phẩmi!';
                    }
                }

                if ($message == '') {
                    $class = 'msg msg-ok msg-hidden';
                } else {
                    $class = 'msg msg-ok';
                }
                ?>		
                <div class="<?php echo($class); ?>" id="msgOK">
                    <p><strong><?php echo($message); ?></strong></p>
                    <a href="#" onclick="hideMessage()" class="close">close</a>
                </div>
                <!-- End Message OK -->		

                <!-- Message Error -->
                <div class="msg msg-error msg-hidden">
                    <p><strong>You must select a file to upload first!</strong></p>
                    <a href="#" class="close">close</a>
                </div>
                <!-- End Message Error -->
                <br />
                <!-- Main -->
                <div id="main">
                    <div class="cl">&nbsp;</div>

                    <!-- Content -->
                    <div id="content">

                        <!-- Box -->
                        <div class="box">
                            <!-- Box Head -->
                            <div class="box-head">
                                <form method="get" action="product.php" id="form">
                                    <h2 class="left">Danh Sách Sản Phẩm</h2> 
                                    <select class="left small-field middle-dropdown" id="cateID" name="cateID" onchange="document.forms[0].submit();" >
                                        <option value="">Tất Cả</option>
                                        <?php
                                        $categoryResult = getNonParentCategory();
                                        while ($row = mysql_fetch_array($categoryResult)) {
                                            if ($row[0] != $cateID) {
                                                ?>
                                                <option value="<?php echo($row[0]) ?>"><?php echo($row[2]); ?></option>
                                                <?php
                                            } else {
                                                ?>
                                                <option selected="selected" value="<?php echo($row[0]) ?>"><?php echo($row[2]); ?></option>
                                                <?php
                                            }
                                        }
                                        ?>

                                    </select>
                                    <div class="right">
                                        <label>Tìm Kiếm:</label>
                                        <input type="text" class="field small-field" name="keyword" id="keyword" value="<?php echo($keyword); ?>" />
                                    </div>
                            </div>
                            <!-- End Box Head -->	

                            <!-- Table -->
                            <div class="table">
                                <table width="100%" border="0" cellspacing="0" cellpadding="0">
                                    <tr>
                                        <th width="13"><input type="checkbox" class="checkbox" onclick="toggle(this)"/></th>
                                        <th>STT</th>
                                        <th>Tên Sản Phẩm</th>
                                        <th>Ngày Tạo</th>
                                        <th>Tình Trạng</th>
                                        <th>Phân Loại</th>
                                    </tr>
                                    <?php
                                    $statusArray = array(1 => 'Còn Hàng', 2 => 'Hàng Mới Về', 3 => 'Hết Hàng', 4 => 'Hàng Sắp Về', 5 => 'Sale off', 6 => 'Ẩn');
                                    $result = getProducts($keyword, $cateID, $start_position, $row_per_page);
                                    $counter = 1;
                                    while ($row = mysql_fetch_array($result)) {
                                        $cate = getCategoryByID($row["CAT_ID"]);
                                        ?>
                                        <tr>
                                            <td><input type="checkbox" class="checkbox" name="pro_id[]" value="<?php echo($row['PRO_ID']); ?>"/></td>
                                            <td><?php echo($counter); ?></td>
                                            <td><h3><a href="productdetail.php?id=<?php echo($row['PRO_ID']); ?>"><?php echo($row['PRO_NAME']); ?></a></h3></td>
                                            <td><?php echo($row["PRO_ADDEDDATE"]); ?></td>
                                            <td><?php echo($statusArray[$row['PRO_STATUS']]); ?></td>
                                            <td><a href="categoryDetail.php?catID=<?php echo($cate['CAT_ID']);?>"><?php echo($cate[2]); ?></a></td>
                                        </tr>
                                        <?php
                                        $counter++;
                                    }
                                    ?>
                                </table>


                                <!-- Pagging -->
                                <div class="pagging">
                                    <div class="left">Trang <?php echo(" " . $page_num . "  trong tổng số " . $total_page . " trang "); ?></div>
                                    <div class="right">

                                        <?php
                                        for ($i = 1; $i <= $total_page; $i++) {
                                            echo("<a" . ($page_num == $i ? " class='currentPage'" : "") . " href='" . "product.php?cateID=" . $cateID . "&keyword=" . $keyword . "&page_num=" . $i . "'>" . $i . "</a>");
                                        }
                                        ?>


                                    </div>
                                </div>
                                <!-- End Pagging -->

                            </div>
                            <!-- Table -->

                        </div>
                        <!-- End Box -->

                        <!-- Box -->
                        <div class="box">
                            <!-- Box Head -->
                            <div class="box-head">
                                <h2>Thao tác</h2>
                            </div>
                            <!-- End ofBox Head -->
                            <div class="box-content">
                                <div class="buttons">
                                    <input type="button" class="button" value="Thêm Sản Phẩm" onclick="window.location = 'new_product.php'"/> &nbsp;
                                    <input type="button" class="button" onclick="submitDelete();" value="Xóa Sản Phẩm"/> 
                                </div>
                            </div>
                            </form>
                        </div>
                    </div>
                    <!-- End Content -->



                    <div class="cl">&nbsp;</div>			
                </div>
                <!-- Main -->
            </div>
        </div>
        <!-- End Container -->

        <!-- Footer -->
        <div id="footer">
            <?php include("footer.php"); ?>
        </div>
        <!-- End Footer -->

    </body>
</html>